using ServiceStack.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ServiceStack.Support
{
	public class InMemoryLog : ILog
	{
		private readonly object syncLock = new object();
		public string LoggerName
		{
			get;
			private set;
		}
		public StringBuilder CombinedLog
		{
			get;
			private set;
		}
		public List<string> DebugEntries
		{
			get;
			set;
		}
		public List<Exception> DebugExceptions
		{
			get;
			set;
		}
		public List<string> InfoEntries
		{
			get;
			set;
		}
		public List<Exception> InfoExceptions
		{
			get;
			set;
		}
		public List<string> WarnEntries
		{
			get;
			set;
		}
		public List<Exception> WarnExceptions
		{
			get;
			set;
		}
		public List<string> ErrorEntries
		{
			get;
			set;
		}
		public List<Exception> ErrorExceptions
		{
			get;
			set;
		}
		public List<string> FatalEntries
		{
			get;
			set;
		}
		public List<Exception> FatalExceptions
		{
			get;
			set;
		}
		public bool HasExceptions
		{
			get
			{
				return this.DebugExceptions.Count > 0 || this.InfoExceptions.Count > 0 || this.WarnExceptions.Count > 0 || this.ErrorExceptions.Count > 0 || this.FatalExceptions.Count > 0;
			}
		}
		public bool IsDebugEnabled
		{
			get;
			set;
		}
		public InMemoryLog(string loggerName)
		{
			this.LoggerName = loggerName;
			this.CombinedLog = new StringBuilder();
			this.DebugEntries = new List<string>();
			this.DebugExceptions = new List<Exception>();
			this.InfoEntries = new List<string>();
			this.InfoExceptions = new List<Exception>();
			this.WarnEntries = new List<string>();
			this.WarnExceptions = new List<Exception>();
			this.ErrorEntries = new List<string>();
			this.ErrorExceptions = new List<Exception>();
			this.FatalEntries = new List<string>();
			this.FatalExceptions = new List<Exception>();
		}
		private void AppendToLog(ICollection<string> logEntries, string format, params object[] args)
		{
			if (format == null)
			{
				return;
			}
			this.AppendToLog(logEntries, string.Format(format, args));
		}
		private void AppendToLog(ICollection<string> logEntries, object message)
		{
			if (message == null)
			{
				return;
			}
			this.AppendToLog(logEntries, message.ToString());
		}
		private void AppendToLog(ICollection<string> logEntries, ICollection<Exception> logExceptions, object message, Exception ex)
		{
			if (ex != null)
			{
				object obj = this.syncLock;
				lock (obj)
				{
					logExceptions.Add(ex);
				}
			}
			if (message == null)
			{
				return;
			}
			this.AppendToLog(logEntries, message.ToString());
		}
		private void AppendToLog(ICollection<string> logEntries, string message)
		{
			bool flag = false;
			try
			{
				Monitor.Enter(this, ref flag);
				logEntries.Add(message);
				this.CombinedLog.AppendLine(message);
			}
			finally
			{
				if (flag)
				{
					Monitor.Exit(this);
				}
			}
		}
		public void Debug(object message)
		{
			if (this.IsDebugEnabled)
			{
				this.AppendToLog(this.DebugEntries, message);
			}
		}
		public void Debug(object message, Exception exception)
		{
			if (this.IsDebugEnabled)
			{
				this.AppendToLog(this.DebugEntries, this.DebugExceptions, message, exception);
			}
		}
		public void DebugFormat(string format, params object[] args)
		{
			if (this.IsDebugEnabled)
			{
				this.AppendToLog(this.DebugEntries, format, args);
			}
		}
		public void Error(object message)
		{
			this.AppendToLog(this.ErrorEntries, message);
		}
		public void Error(object message, Exception exception)
		{
			this.AppendToLog(this.ErrorEntries, this.ErrorExceptions, message, exception);
		}
		public void ErrorFormat(string format, params object[] args)
		{
			this.AppendToLog(this.ErrorEntries, format, args);
		}
		public void Fatal(object message)
		{
			this.AppendToLog(this.FatalEntries, message);
		}
		public void Fatal(object message, Exception exception)
		{
			this.AppendToLog(this.FatalEntries, this.FatalExceptions, message, exception);
		}
		public void FatalFormat(string format, params object[] args)
		{
			this.AppendToLog(this.FatalEntries, format, args);
		}
		public void Info(object message)
		{
			this.AppendToLog(this.InfoEntries, message);
		}
		public void Info(object message, Exception exception)
		{
			this.AppendToLog(this.InfoEntries, this.InfoExceptions, message, exception);
		}
		public void InfoFormat(string format, params object[] args)
		{
			this.AppendToLog(this.InfoEntries, format, args);
		}
		public void Warn(object message)
		{
			this.AppendToLog(this.WarnEntries, message);
		}
		public void Warn(object message, Exception exception)
		{
			this.AppendToLog(this.WarnEntries, this.WarnExceptions, message, exception);
		}
		public void WarnFormat(string format, params object[] args)
		{
			this.AppendToLog(this.WarnEntries, format, args);
		}
	}
}
